package cn._tedu.eguser1.eguser1.controller;

import cn._tedu.eguser1.eguser1.base.response.Result;
import cn._tedu.eguser1.eguser1.mapper.UserMapper;
import cn._tedu.eguser1.eguser1.pojo.dto.UserAddParam;
import cn._tedu.eguser1.eguser1.pojo.dto.UserLoginParam;
import cn._tedu.eguser1.eguser1.pojo.dto.UserUpdateParam;
import cn._tedu.eguser1.eguser1.pojo.entity.User;
import cn._tedu.eguser1.eguser1.pojo.vo.UserListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author tune_the last
 * @date 2025/11/12 - 14:43
 */
@RestController
@RequestMapping("/v1/user")
@Tag(name = "用户管理")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @ApiOperationSupport(order = 10)
    @Operation(summary = "添加用户")
    @PostMapping("/add")
    public String addUser(@RequestBody @Validated UserAddParam userAddParam){
        User user = new User();
        BeanUtils.copyProperties(userAddParam, user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        int i = userMapper.insertUser(user);
        if (i > 0){
            return "添加用户成功";
        }
        return "添加用户失败";
    }
    @ApiOperationSupport(order = 20)
    @Operation(summary = "用户列表")
    @GetMapping("/list")
    public Result getUserList(){
        List<UserListVO> userListVOList = userMapper.getList();
        return Result.ok(userListVOList);
    }
    @ApiOperationSupport(order = 30)
    @Operation(summary = "删除用户")
    @PostMapping("/del/{id}")
    public String deleteUser(@PathVariable("id") Long id){
        if (id > 0){
            throw new IllegalArgumentException("用户id不能小于0");
        }
        int i = userMapper.deleteUserById(id);
        if(i > 0){
            return "删除id为" + id + "的用户成功";
        }
        return "删除用户失败或该用户不存在";
    }
    @ApiOperationSupport(order = 30)
    @Operation(summary = "更新用户")
    @PostMapping("/update")
    public String updateUser(@RequestBody UserUpdateParam userUpdateParam){
        User user = new User();
        BeanUtils.copyProperties(userUpdateParam, user);
        user.setUpdateTime(new Date());
        int i = userMapper.updateUserById(user);
        if(i > 0){
            return "更新id为" + user.getId() + "的用户信息成功";
        }
        return "更新用户信息失败或该用户不存在";
    }
    @ApiOperationSupport(order = 40)
    @Operation(summary = "登录功能")
    @PostMapping("/login")
    public String login(@RequestBody UserLoginParam param){
        System.out.println("param = " + param);

        return "登录成功";
    }


}
